import Request from '@/request/request.js'
import { getToken, setToken, removeToken, saveUserInfo, loadUserinfo } from '@/utils/ProjectTools.js'
import {RolesList} from "../../../mock/data/role";
import { http } from "@/api";

// 重置用户令牌和角色信息
const state = {
    userInfo: loadUserinfo(),
    token: getToken(),
    valid: false, // 当前路由是否有效
    roles: {}, //角色信息
    // 商户信息
    merchantInfo: null,
    merchantId: ''
}
const getters = {
    userInfo: state => state.userInfo,
}
const mutations = {
    SAVE_AUTHEN_INFO: (state, data) => {
        setToken(data.token)
        saveUserInfo(data.userInfo)
        state.token = data.token;
        state.userInfo = data.userInfo
    },
    LOGOUT(state) {
        removeToken();
        state.userInfo = {};
        state.merchantInfo = null;
        state.token = '';
        state.valid = false;
        state.roles = null;
    },
    USER_ROLES: (state, data) => {
        state.roles = data;
    },
    SET_VALID: (state) => {
        state.valid = true;
    },
    // 设置商户信息
    SET_MERCHANT_INFO: (state, data) => {
        state.merchantInfo = data
    },
    SET_USER_ID: (state, data)  => {
        state.merchantId = data
    }
};

const actions = {
    // 获取验证码
    getLoginCaptcha() {
        return Request.get("/public/admin/captcha")
            .then(res => {
                return Promise.resolve(res.data);
            })
            .catch(err => {
                return Promise.reject(err);
            });

    },
    // 用户登录
    login({ commit, dispatch }, userInfo) {
        const { username, password, code } = userInfo
        console.log('userInfo',userInfo)
        return new Promise(async (resolve, reject) => {
            http.login({
                username,
                password,
                code
            }).then(async res => {
                if (res.code === 401) {
                    reject(res)
                    return
                }
                let result = {
                    token: res.data.token,
                    userInfo: res.data
                }
                // 保存状态
                commit('SAVE_AUTHEN_INFO', result);
                // let menus = await dispatch('getUserRoles', res.data.userInfo.roleId)
                resolve({ userInfo: res.userInfo, menus: [] })
            }).catch((err) => {
                console.log(err)
            })
        })
    },
    // 重置令牌
    resetToken({ commit,dispatch }) {
        return new Promise((resolve, reject) => {
            dispatch('tagsView/delAllViews',null,{ root: true })
            commit('LOGOUT')
            // Request({
            //     url: '/public/admin/login/out',
            //     method: 'get',
            // }).then(()=>{
            //     commit('LOGOUT')
            //     resolve()
            // }).catch(()=>{
            //     commit('LOGOUT')
            //     resolve()
            // })
        })
    },
    // 当前路由是否有效
    setValid({ commit }) {
        commit('SET_VALID')
    },
    // 获取用户角色
    getUserRoles({ commit }, id) {
        return new Promise((resolve, reject) => {
            Request({
                url: '/private/roles/one',
                method: 'post',
                data: {
                    params: { id }
                },
            }).then(res => {
                commit("USER_ROLES", res.data);
                resolve(res.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 获取商户信息
    getMerchantInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            http.merchant({
                id: state.userInfo.id
            }).then(res => {
                commit('SET_MERCHANT_INFO', res.data)
                commit('SET_USER_ID', res.data.id)
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}
